<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="竞争单位名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入竞争单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书名称" prop="certificateName">
        <el-input
          v-model="queryParams.certificateName"
          placeholder="请输入证书名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资质类别" prop="certificateType">
        <el-select v-model="queryParams.certificateType" placeholder="请选择资质类别" clearable>
          <el-option
            v-for="dict in dict.type.pm_compete_qualit_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资质等级" prop="certificateGrade">
        <el-select v-model="queryParams.certificateGrade" placeholder="请选择资质等级" clearable>
          <el-option
            v-for="dict in dict.type.pm_award_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:compete_enterprise_certificate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:compete_enterprise_certificate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:compete_enterprise_certificate:remove']"
        >删除</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:compete_enterprise_certificate:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="compete_enterprise_certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="竞争单位名称" align="left" header-align="center" prop="enterpriseName" width="210" fixed/>
      <el-table-column label="证书名称" align="center" prop="certificateName" />
      <el-table-column label="资质类别" align="center" prop="certificateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_compete_qualit_category" :value="scope.row.certificateType"/>
        </template>
      </el-table-column>
      <el-table-column label="资质等级" align="center" prop="certificateGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_award_grade" :value="scope.row.certificateGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="资质等级图片" align="center" prop="certificateImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.certificateImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:compete_enterprise_certificate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:compete_enterprise_certificate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资质证书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="竞争单位名称" prop="enterpriseId">
          <el-select
            v-model="form.enterpriseId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择竞争单位名称"
            :remote-method="getCompeteEnterpriseList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="证书名称" prop="certificateName" style="width: 320px">
          <el-input v-model="form.certificateName" placeholder="请输入证书名称" />
        </el-form-item>
        <el-form-item label="资质类别" prop="certificateType">
          <el-select v-model="form.certificateType" placeholder="请选择资质类别">
            <el-option
              v-for="dict in dict.type.pm_compete_qualit_category"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资质等级" prop="certificateGrade">
          <el-select v-model="form.certificateGrade" placeholder="请选择资质等级">
            <el-option
              v-for="dict in dict.type.pm_award_grade"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资质等级图片">
          <image-upload v-model="form.certificateImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompete_enterprise_certificate, getCompete_enterprise_certificate, delCompete_enterprise_certificate, addCompete_enterprise_certificate, updateCompete_enterprise_certificate } from "@/api/business/compete_enterprise_certificate";
import { optionList} from "@/api/business/compete_enterprise";

export default {
  name: "Compete_enterprise_certificate",
  dicts: ['pm_award_grade', 'pm_compete_qualit_category'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资质证书表格数据
      compete_enterprise_certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options: [],
      //竞争企业查询参数
      queryEnterpriseParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        certificateName: null,
        certificateType: null,
        certificateGrade: null,
        certificateImage: null,
        enterpriseId: null
      },
      option: {
        enterpriseId: null,
        enterpriseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: "竞争企业名称不能为空", trigger: "blur" }
        ],
        certificateName: [
          { required: true, message: "证书名称不能为空", trigger: "blur" }
        ],
        certificateType: [
          { required: true, message: "资质类别不能为空", trigger: "change" }
        ],
        certificateGrade: [
          { required: true, message: "资质等级不能为空", trigger: "change" }
        ],
        certificateImage: [
          { required: true, message: "资质等级图片不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const tempEnterpriseId = this.$route.params && this.$route.params.enterpriseId;
    if(tempEnterpriseId!=null){
      this.queryParams.enterpriseId = tempEnterpriseId;
    }
    this.getList();
  },
  methods: {
    /** 查询资质证书列表 */
    getList() {
      this.loading = true;
      listCompete_enterprise_certificate(this.queryParams).then(response => {
        this.compete_enterprise_certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCompeteEnterpriseList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryEnterpriseParams.enterpriseName = query;
        optionList(this.queryEnterpriseParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        certificateId: null,
        enterpriseId: null,
        certificateName: null,
        certificateType: null,
        certificateGrade: null,
        options: [],
        certificateImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.certificateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加竞争单位资质证书";
      this.loading = true;
      this.queryEnterpriseParams.enterpriseName = '';
      optionList(this.queryEnterpriseParams).then(response => {
        this.loading = false;
        this.options = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.option.enterpriseId=row.enterpriseId;
      this.option.enterpriseName=row.enterpriseName;
      this.options.push(this.option);
      const certificateId = row.certificateId || this.ids
      getCompete_enterprise_certificate(certificateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改竞争单位资质证书";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.certificateId != null) {
            updateCompete_enterprise_certificate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompete_enterprise_certificate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const certificateIds = row.certificateId || this.ids;
      var deleteTitle = row.certificateName? ('"'+row.certificateName+'"'):'';
      this.$modal.confirm('是否确认删除' + deleteTitle + '？').then(function() {
        return delCompete_enterprise_certificate(certificateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/compete_enterprise_certificate/export', {
        ...this.queryParams
      }, `compete_enterprise_certificate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
